package com.ruoyi.web.controller.module;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.AdBanner;
import com.ruoyi.system.domain.module.Advert;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.IAdvertService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 广告列Controller
 *
 * @author ruoyi
 * @date 2021-08-14
 */
@Validated
@Api(value = "广告列控制器", tags = {"广告列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/advert")
public class AdvertController extends BaseController {

    private final IAdvertService advertService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询广告列列表
     */
    @ApiOperation("查询广告列列表")
    @PreAuthorize("@ss.hasPermi('module:advert:list')")
    @GetMapping("/list")
    public TableDataInfo<Advert> list(@Validated Advert advert) {
		TableDataInfo<Advert> td = PageUtils.buildDataInfo(advertService.page(PageUtils.buildPage()));
		List<Advert> list = td.getRows();
		list.forEach(advert1->{
			if(StrUtil.isNotEmpty(advert1.getPreviewImg())){
				advert1.setPreviewPath(imgUrl + advert1.getPreviewImg());
			}
		});
        return td;
    }

    /**
     * 获取广告列详细信息
     */
    @ApiOperation("获取广告列详细信息")
    @PreAuthorize("@ss.hasPermi('module:advert:query')")
    @GetMapping("/{id}")
    public AjaxResult<Advert> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
		Advert advert = advertService.getById(id);
		if(advert!=null){
			if(StrUtil.isNotEmpty(advert.getPreviewImg())){
				advert.setPreviewPath(imgUrl + advert.getPreviewImg());
			}else{
				advert.setPreviewPath("");
			}
		}
        return AjaxResult.success(advert);
    }

    /**
     * 新增广告列
     */
    @ApiOperation("新增广告列")
    @PreAuthorize("@ss.hasPermi('module:advert:add')")
    @Log(title = "广告列", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody Advert advert) {
        return toAjax(advertService.save(advert) ? 1 : 0);
    }

    /**
     * 修改广告列
     */
    @ApiOperation("修改广告列")
    @PreAuthorize("@ss.hasPermi('module:advert:edit')")
    @Log(title = "广告列", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody Advert advert) {
        return toAjax(advertService.updateById(advert) ? 1 : 0);
    }

    /**
     * 删除广告列
     */
    @ApiOperation("删除广告列")
    @PreAuthorize("@ss.hasPermi('module:advert:remove')")
    @Log(title = "广告列" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(advertService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
